﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IceCS.Helpers
{
    /// <summary>
    /// 
    /// </summary>
    public class FileLogHelper
    {
        /// <summary>
        /// 写入日志
        /// </summary>
        /// <param name="message">日志内容</param>
        /// <param name="group">分组</param>
        public void WriteLog(string message, string group = "")
        {
            var basePath = AppContext.BaseDirectory;
            var logPath = System.IO.Path.Combine(basePath, "error-log");
            if (!System.IO.Directory.Exists(logPath)) { System.IO.Directory.CreateDirectory(logPath); }
            if (System.IO.File.Exists(logPath + "off.log")) { return; }
            var now = System.DateTime.Now;
            var fileName = now.ToString("yyyy-MM-dd") + ".log";
            if (string.IsNullOrEmpty(group)) { group = "default"; } else { fileName = string.Concat(group, "-", fileName); }
            fileName = System.IO.Path.Combine(logPath, fileName);
            var sw = new System.IO.StreamWriter(fileName, true) { AutoFlush = true };
            var head = "[START]--[Group:" + group + "]--[Time:" + now.ToString("yyyy-MM-dd HH:mm:ss.FFF") + "]--[START]" + "\r\n";
            var content = "[LOG]:" + message + "\r\n";
            var foot = "[END]--[Group:" + group + "]--[END]\r\n\r\n";
            var msg = head + content + foot;
            sw.Write(msg);
            sw.Close();
        }
    }
}
